package tencent_im

import (
	"net/http"

	"github.com/gin-gonic/gin"
)

type JsonResult struct {
	Code      uint32      `json:"ErrorCode"`    // 响应编码：0成功 401请登录 403无权限 500错误
	Msg       string      `json:"ActionStatus"` // 消息提示语
	ErrorInfo string      `json:"ErrorInfo"`
	Data      interface{} `json:"MsgBody"` // 数据对象
}

func ResponseJsonMap(code uint32, message string, data interface{}) JsonResult {
	jsonMap := grantMap(code, message, data)
	return jsonMap
}

// 按照接口格式生成原数据数组
func grantMap(code uint32, message string, data interface{}) JsonResult {
	jsonMap := JsonResult{
		Code:      code,
		Msg:       message,
		ErrorInfo: "",
		Data:      data,
	}
	return jsonMap
}

// 获取全部请求解析到map
func Response(c *gin.Context, code uint32, msg string, data interface{}) {
	message := ResponseJsonMap(code, msg, data)
	c.Set("content-type", "application/json")
	c.JSON(http.StatusOK, message)
}

// 成功
func Success(c *gin.Context, code uint32, msg string, data interface{}) {
	Response(c, code, msg, data)
}

// 失败
func Error(c *gin.Context, msg string, data interface{}) {
	Response(c, 50000, msg, data)
}
